//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------
#include "KeyString.h"
#include "Clipboard.h"

#include <Windows.h>


//const std::string KeyString::DELIMITERS(" \t\n\r");
//const int KeyString::DELIMITERS(" \t\n\r");
const int KeyString::UNKNOWN = 0;
const int KeyString::RETURN = 1;
const int KeyString::DELETE_ = 2;
const int KeyString::BACKSLASH = 3;
const int KeyString::LEFT = 4;
const int KeyString::RIGHT = 5;
const int KeyString::COPY = 6;
const int KeyString::PASTE = 7;
const int KeyString::KEY = 8;


KeyString::KeyString() {

}

KeyString::~KeyString() {

}

int KeyString::Update(std::string& text, int& indexStart, int& indexEnd, KeyState keyState) {
	if(keyState.vkey == VK_RETURN) { //ENTER
		return RETURN;
	}

	if(keyState.vkey == VK_DELETE) { //ENTF
		int length = text.length()-1;
		if(length >= 0) {
			//cut one char.
			if(indexEnd == indexStart) {
				if(indexEnd < text.length()) {
					//1234|56789 -> 123|56789 //cut the area out and replace it with the new char.
					text = text.substr(0, indexEnd) + text.substr(indexEnd+1, text.length()); //slice
                 }
			}

			//cut area of chars.
            //determine which are the min and which are the max index.
            int minIndex = min(indexStart, indexEnd);
            int maxIndex = max(indexStart, indexEnd);

            //123[456]789 -> 123A789  , [ = minIndex, ] = maxIndex //cut the area out and replace it with the new char.
            text = text.substr(0, minIndex) + text.substr(maxIndex, text.length());

            //raise the text indexes.
            indexEnd = minIndex;
            indexStart = minIndex;

			return DELETE_;
		}

		return UNKNOWN;
	}

	if(keyState.vkey == VK_BACK) { //delete
		int length = text.length()-1;
		if(length >= 0) {
			//cut one char.
			if(indexEnd == indexStart) {
				if(indexEnd > 0) {
					//1234|56789 -> 123|56789 //cut the area out and replace it with the new char.
					text = text.substr(0, indexEnd-1) + text.substr(indexEnd, text.length()); //slice

                    indexEnd--;
                    indexStart--;
                 }
			}

			//cut area of chars.
            //determine which are the min and which are the max index.
            int minIndex = min(indexStart, indexEnd);
            int maxIndex = max(indexStart, indexEnd);

            //123[456]789 -> 123A789  , [ = minIndex, ] = maxIndex //cut the area out and replace it with the new char.
            text = text.substr(0, minIndex) + text.substr(maxIndex, text.length());

            //raise the text indexes.
            indexEnd = minIndex;
            indexStart = minIndex;

            return BACKSLASH;
		}

        return UNKNOWN;
	} else if(keyState.vkey == VK_LEFT) {
		int minIndex = min(indexStart, indexEnd);

		indexStart = minIndex-1;
		indexEnd = minIndex-1;
		if(indexStart < 0 || indexEnd < 0) {
			indexStart = 0;
			indexEnd = 0;
		}

		return LEFT;

	} else if(keyState.vkey == VK_RIGHT) {
		int maxIndex = max(indexStart, indexEnd);

		indexStart = maxIndex+1;
		indexEnd = maxIndex+1;
		if(indexStart > text.length() || indexEnd > text.length()) {
			indexStart = text.length();
			indexEnd = text.length();
		}

		return RIGHT;
	}

	if(keyState.ctrlPressed && keyState.vkey == 67) { //strg + c (copy)
		int minIndex = min(indexStart, indexEnd);
		int maxIndex = max(indexStart, indexEnd);

		std::string tempString = text.substr(minIndex, maxIndex);

		Clipboard::CopyToClipboard(tempString);
		
		return COPY;
	}

	//TODO:
	if(keyState.ctrlPressed && keyState.vkey == 86) { //strg + v (paste)
		int minIndex = min(indexStart, indexEnd);
		int maxIndex = max(indexStart, indexEnd);

		std::string tempString = Clipboard::ClipboardToString();
		text = text.substr(0, minIndex) + tempString + text.substr(maxIndex, text.length());

		return PASTE;
	}

	//
	char charCode = keyState.ckey; // e._charCode;
	std::string charCode_str = "";
	charCode_str.insert(0, 1, charCode);

	if(charCode == '\0') return UNKNOWN;

    //determine the min and the max index.
    int minIndex = min(indexStart, indexEnd);
    int maxIndex = max(indexStart, indexEnd);

    //cut the area out and replace it with the new char.  123[456]789 -> 123A789  , [ = minIndex, ] = maxIndex
    text = text.substr(0, minIndex) + charCode_str + text.substr(maxIndex, text.length());

	indexEnd = minIndex+1;
	indexStart = minIndex+1;

	return KEY;
}